#include <stdio.h>
#include <stdlib.h>
#include "lib/cjson/cJSON.h"

char* get_rps_error(int cmd_id, char* cmd_method, char* cmd_vers, int err_code, char* err_msg) {
    cJSON* pJsonRoot = NULL;
    pJsonRoot = cJSON_CreateObject();
    if(NULL == pJsonRoot) {
        printf("cJSON_CreateObject failed\n");
        return NULL;
    }
    cJSON_AddNumberToObject(pJsonRoot, "id", cmd_id);
    cJSON_AddStringToObject(pJsonRoot, "method", cmd_method);
    cJSON_AddStringToObject(pJsonRoot, "jsonrpc", cmd_vers);
    cJSON* pSubJson = NULL;
    pSubJson = cJSON_CreateObject();
    if(NULL == pSubJson) {
        cJSON_Delete(pJsonRoot);
        return NULL;
    }
    cJSON_AddNumberToObject(pSubJson, "code", err_code);
    cJSON_AddStringToObject(pSubJson,"debugMsg", err_msg);
    cJSON_AddItemToObject(pJsonRoot, "error", pSubJson);
    char* p = cJSON_Print(pJsonRoot);
    if(NULL == p) {
        cJSON_Delete(pJsonRoot);
        return NULL;
    }
    cJSON_Delete(pJsonRoot);
    return p;
}


char* get_rps_send_master_info(int cmd_id, char* cmd_method, char* cmd_vers) {
    cJSON* pJsonRoot = NULL;
    pJsonRoot = cJSON_CreateObject();
    if(NULL == pJsonRoot) {
        printf("cJSON_CreateObject failed\n");
        return NULL;
    }
    cJSON_AddNumberToObject(pJsonRoot, "id", cmd_id);
    cJSON_AddStringToObject(pJsonRoot, "method", cmd_method);
    cJSON_AddStringToObject(pJsonRoot, "jsonrpc", cmd_vers);
    cJSON* pSubJson = NULL;
    pSubJson = cJSON_CreateObject();
    if(NULL == pSubJson) {
        cJSON_Delete(pJsonRoot);
        return NULL;
    }
    cJSON_AddNumberToObject(pSubJson, "status", 1);
    cJSON_AddItemToObject(pJsonRoot, "result", pSubJson);
    char* p = cJSON_Print(pJsonRoot);
    if(NULL == p) {
        cJSON_Delete(pJsonRoot);
        return NULL;
    }
    cJSON_Delete(pJsonRoot);
    return p;
}


char* get_rps_send_wifi_info(int cmd_id, char* cmd_method, char* cmd_vers) {
    cJSON* pJsonRoot = NULL;
    pJsonRoot = cJSON_CreateObject();
    if(NULL == pJsonRoot) {
        printf("cJSON_CreateObject failed\n");
        return NULL;
    }
    cJSON_AddNumberToObject(pJsonRoot, "id", cmd_id);
    cJSON_AddStringToObject(pJsonRoot, "method", cmd_method);
    cJSON_AddStringToObject(pJsonRoot, "jsonrpc", cmd_vers);
    cJSON* pSubJson = NULL;
    pSubJson = cJSON_CreateObject();
    if(NULL == pSubJson) {
        cJSON_Delete(pJsonRoot);
        return NULL;
    }
    cJSON_AddNumberToObject(pSubJson, "status", 1);
    cJSON_AddItemToObject(pJsonRoot, "result", pSubJson);
    char* p = cJSON_Print(pJsonRoot);
    if(NULL == p) {
        cJSON_Delete(pJsonRoot);
        return NULL;
    }
    cJSON_Delete(pJsonRoot);
    return p;
}


char* get_rps_modify_device_wifi(int cmd_id, char* cmd_method, char* cmd_vers) {
    cJSON* pJsonRoot = NULL;
    pJsonRoot = cJSON_CreateObject();
    if(NULL == pJsonRoot) {
        printf("cJSON_CreateObject failed\n");
        return NULL;
    }
    cJSON_AddNumberToObject(pJsonRoot, "id", cmd_id);
    cJSON_AddStringToObject(pJsonRoot, "method", cmd_method);
    cJSON_AddStringToObject(pJsonRoot, "jsonrpc", cmd_vers);
    cJSON* pSubJson = NULL;
    pSubJson = cJSON_CreateObject();
    if(NULL == pSubJson) {
        cJSON_Delete(pJsonRoot);
        return NULL;
    }
    cJSON_AddNumberToObject(pSubJson, "status", 1);
    cJSON_AddItemToObject(pJsonRoot, "result", pSubJson);
    char* p = cJSON_Print(pJsonRoot);
    if(NULL == p) {
        cJSON_Delete(pJsonRoot);
        return NULL;
    }
    cJSON_Delete(pJsonRoot);
    return p;
}

char* get_rps_get_device_ulist(int cmd_id, char* cmd_method, char* cmd_vers) {
    cJSON* pJsonRoot = NULL;
    pJsonRoot = cJSON_CreateObject();
    if(NULL == pJsonRoot) {
        printf("cJSON_CreateObject failed\n");
        return NULL;
    }

    cJSON_AddNumberToObject(pJsonRoot, "id", cmd_id);
    cJSON_AddStringToObject(pJsonRoot, "method", cmd_method);
    cJSON_AddStringToObject(pJsonRoot, "jsonrpc", cmd_vers);

    cJSON* pSubJson = NULL;
    pSubJson = cJSON_CreateObject();
    if(NULL == pSubJson) {
        cJSON_Delete(pJsonRoot);
        return NULL;
    }

    cJSON* ppSubJson = cJSON_CreateArray();
    if(NULL == ppSubJson) {
        cJSON_Delete(pJsonRoot);
        return NULL;
    }

    int i = 0;
    for(; i < 6; i++) {
        cJSON* pppSubJson = cJSON_CreateObject();
        if(NULL == pppSubJson) {
            cJSON_Delete(pJsonRoot);
            return NULL;
        }
        cJSON_AddStringToObject(pppSubJson, "deviceName", "dog");
        cJSON_AddStringToObject(pppSubJson, "deviceMac", "1FC12D156E78F730B84C539B27B4796B");
        cJSON_AddItemToArray(ppSubJson, pppSubJson);
    }


    cJSON_AddItemToObject(pSubJson, "deviceUserList", ppSubJson);
    cJSON_AddItemToObject(pJsonRoot, "result", pSubJson);
    char* p = cJSON_Print(pJsonRoot);
    if(NULL == p) {
        cJSON_Delete(pJsonRoot);
        return NULL;
    }

    cJSON_Delete(pJsonRoot);
    return p;
}

char* get_rps_delete_device_user(int cmd_id, char* cmd_method, char* cmd_vers) {
    cJSON* pJsonRoot = NULL;
    pJsonRoot = cJSON_CreateObject();
    if(NULL == pJsonRoot) {
        printf("cJSON_CreateObject failed\n");
        return NULL;
    }
    cJSON_AddNumberToObject(pJsonRoot, "id", cmd_id);
    cJSON_AddStringToObject(pJsonRoot, "method", cmd_method);
    cJSON_AddStringToObject(pJsonRoot, "jsonrpc", cmd_vers);
    cJSON* pSubJson = NULL;
    pSubJson = cJSON_CreateObject();
    if(NULL == pSubJson) {
        cJSON_Delete(pJsonRoot);
        return NULL;
    }
    cJSON_AddNumberToObject(pSubJson, "status", 1);
    cJSON_AddItemToObject(pJsonRoot, "result", pSubJson);
    char* p = cJSON_Print(pJsonRoot);
    if(NULL == p) {
        cJSON_Delete(pJsonRoot);
        return NULL;
    }
    cJSON_Delete(pJsonRoot);
    return p;
}
